package com.learn.quartz.factory;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

/**
 * 当配置这个config后,quartz才可以使用autowired注入
 * 注：只有有这个配置，quartz才可以使用自动注入，不然quartz无法使用Autowired
 * @author: MI
 * @email: 448341911@qq.com
 * @createDate: 2023/10/28 23:04
 * @updateUser: MI
 * @updateDate: 2023/10/28 23:04
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@Component
public class ScheduleAutoBeanFactory extends SpringBeanJobFactory implements ApplicationContextAware {


    private transient AutowireCapableBeanFactory beanFactory;


    @Override
    public void setApplicationContext(ApplicationContext context) {
        beanFactory = context.getAutowireCapableBeanFactory();
    }

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        final Object job = super.createJobInstance(bundle);
        beanFactory.autowireBean(job);
        return job;
    }
}
